class StoreController < ApplicationController
  def index
    
    @products = Product.all
    @categories = Category.all
    @showCat = 0 
    @Cat = nil
    @loggedIn = false
    if Admin.find_by_username(session[:user_name])
      @loggedIn = true
    end
    if params[:paramCat] != nil
      @Cat = Category.find_by_id(params[:paramCat])
      catProducts = Product.find(:all, :conditions => {:category_id => params[:paramCat]})
      if catProducts.size > 0
        @products = catProducts
      else
        @products = nil
      end
    end
    render :template => "store/index.html.erb"
    
  end
  
  def register
    render :template => "store/register.html.erb"
  end
  
  def register_admin
    @admin = Admin.new(params[:admin])

    respond_to do |format|
      if @admin.save
        flash[:notice] = 'Admin was successfully created.'
        format.html { redirect_to :action=>"index" }
        format.xml  { render :xml => @admin, :status => :created, :location => @admin }
      else
        format.html { render :action => "register" }
        format.xml  { render :xml => @admin.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def login
    @products = Product.all
    @categories = Category.all
    flash[:notice] = params[:uname]
    if (currentAdmin = Admin.find(:all, :conditions => {:username => params[:admin][:uname], :password => params[:admin][:pw]})).size > 0 
      session[:user_name] = params[:admin][:uname]
      redirect_to :action=>"index"
      flash[:notice] = "Erfolgreich eingeloggt"
    else
      redirect_to :action=>"index"
      flash[:notice] = "Login fehlerhaft!"
    end
  end
  
  def logout
    reset_session
    flash[:notice] = "Sie wurden abgemeldet"
    redirect_to :controller => "store", :action => "index"
  end
  
end
